﻿#region Version Info
/* ========================================================================
* 【功能概述】
* 
* 作者：Zhai Xianwen       时间：2012-3-20 14:18:57
* 邮箱 : zhaisky@163.com
* 
* 文件名：CreatorKey
* 版本：V1.0.1
*
* 修改者：           时间：              
* 修改说明：
* ========================================================================
*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ZXW.DotNet.Common.Basic.Emit.Application
{
    internal class CreatorKey
    {

        #region 字段

        private Type targetType;
        private Type[] paramTypeArray; 

        #endregion

        #region 构造函数

        /// <summary>
        ///   构造函数
        /// </summary>
        public CreatorKey()
        {
        }

        /// <summary>
        ///     构造函数
        /// </summary>
        /// <param name="type" type="System.Type">
        ///     <para>
        ///         类型
        ///     </para>
        /// </param>
        /// <param name="paraTypeAry" type="System.Type[]">
        ///     <para>
        ///         参数
        ///     </para>
        /// </param>
        public CreatorKey(Type type, Type[] paraTypeAry)
        {
            this.targetType = type;
            this.paramTypeArray = paraTypeAry;
        }

        #endregion

        #region 属性

        public Type TargetType
        {
            get { return targetType; }
            set { targetType = value; }
        }


        public Type[] ParamTypeArray
        {
            get { return paramTypeArray; }
            set { paramTypeArray = value; }
        }

        #endregion

        #region Equals ,GetHashCode
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return false;
            }

            if (obj.GetType() != typeof(CreatorKey))
            {
                return false;
            }

            #region Old
            CreatorKey key = (CreatorKey)obj;
            if (key.targetType != this.targetType)
            {
                return false;
            }

            if ((key.paramTypeArray == null) && (this.paramTypeArray != null))
            {
                return false;
            }

            if ((key.paramTypeArray != null) && (this.paramTypeArray == null))
            {
                return false;
            }

            if (key.paramTypeArray.Length != this.paramTypeArray.Length)
            {
                return false;
            }

            for (int i = 0; i < this.paramTypeArray.Length; i++)
            {
                if (this.paramTypeArray[i] != key.paramTypeArray[i])
                {
                    return false;
                }
            }

            return true;
            #endregion
        }

        public override int GetHashCode()
        {
            string hashCodeStr = this.targetType.ToString();
            if (this.paramTypeArray != null)
            {
                for (int i = 0; i < this.paramTypeArray.Length; i++)
                {
                    hashCodeStr += "_" + this.paramTypeArray[i].ToString();
                }
            }

            return hashCodeStr.GetHashCode();
        }
        #endregion

    }
}
